package com.jiyinit.web.core.business.service;

import com.jiyinit.web.core.business.dao.TabCoreGroupDao;
import com.jiyinit.web.core.business.dao.UserGroupDao;
import com.jiyinit.web.core.business.dto.GroupRequest;
import com.jiyinit.web.core.business.entity.TabCoreGroupEntity;
import com.jiyinit.web.core.business.entity.TabUserGroupEntity;
import com.jiyinit.web.core.business.enums.CommonEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by lr on 2017/12/7.
 */
@Service(value = "core.groupService")
public class GroupServiceImpl {

    @Resource(name = "core.userGroupDao")
    private UserGroupDao userGroupDao;

    @Resource(name = "core.groupDao")
    private TabCoreGroupDao tabCoreGroupDao;

    public int addGroup(GroupRequest groupRequest){
        TabCoreGroupEntity group = new TabCoreGroupEntity();
        group.setGroupName(groupRequest.getGroupName());
        group.setCreateId(groupRequest.getUserId());
        group.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
        group.setCreateTime(new Date());
        tabCoreGroupDao.save(group);

        TabUserGroupEntity userGroupEntity = new TabUserGroupEntity();
        userGroupEntity.setUserId(groupRequest.getUserId());
        userGroupEntity.setGroupId(group.getGroupId());
        userGroupEntity.setIdentity(CommonEnum.Identity.HAREN.getCode());
        userGroupEntity.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
        userGroupEntity.setCreateId(groupRequest.getUserId());
        userGroupEntity.setCreateTime(new Date());
        userGroupDao.save(userGroupEntity);
        return  group.getGroupId();
    }

    public TabCoreGroupEntity selectByPrimaryKey(Integer groupId)
    {
        return tabCoreGroupDao.selectByPrimaryKey(groupId);
    }

    public boolean dissolveGroup(Integer groupId){
        //删除群成员
        int userResult=userGroupDao.dissolveGroup(groupId);
        //删除群数据
        int groupResult=tabCoreGroupDao.dissolveGroup(groupId);

        return userResult>0&&groupResult>0;
    }
}
